#include "http.hpp"



void test_split()
{
    // std::string src = "aaa,bbb,ccc,";
    std::string src = ",,,aaa,bbb,,,,,ccc,";
    std::vector<std::string> des;
    Util::Split(src,",",&des);
    for(auto &e : des)
    {
        std::cout<<e<<std::endl;
    }
}

void test_read_write()
{
    std::string out;
    Util::ReadFile("./test.cc",&out);
    //std::cout<<out<<std::endl;

    Util::WriteFile("./tmp.cc",out);

}

void test_url()
{
    std::string url = "c++  =";
    std::string res = Util::UrlEncode(url,true);
    std::cout<<res<<std::endl;
    std::string res2 = Util::UrlDecode(res,true);
    std::cout<<res2<<std::endl;
}

void test_status_mime()
{
    std::cout<<Util::StatusDesc(200) << std::endl;
    std::cout<<Util::ExtMime("xxx.txt") << std::endl;

}

void test_isdir_isreg()
{
    std::cout<<Util::IsDirector("test") << std::endl;
    std::cout<<Util::IsDirector("./") << std::endl;
    std::cout<<Util::IsRegular("tmp.cc") << std::endl;
    std::cout<<Util::IsRegular("./") << std::endl;
}

void test_path()
{
    Util::VlaidPath("/html/index.html");
    std::cout<<Util::VlaidPath("/html/index.html") << std::endl;
    std::cout<<Util::VlaidPath("/../html/index.html") << std::endl;
    std::cout<<Util::VlaidPath("/html/../index.html") << std::endl;
    std::cout<<Util::VlaidPath("/html/../../index.html") << std::endl;
}

void test_context()
{
//      GET /search?q=http&limit=10 HTTP/1.1
//      Host: api.example.com
//      Accept: application/json
//      Cache-Control: no-cache
    Buffer buf;
    buf.WriteStringPush("GET /search?q=http&limit=10 HTTP/1.1\r\nHost: api.example.com\r\nContent-Length: 11\r\n\r\nxuzhengyouxxxxx");
    HttpContext context;
    context.RecvHttpRequest(&buf);
    std::cout<<context.GetRspStatus()<<std::endl;
    std::cout<<context.GetRecvStatus()<<std::endl;
    auto req = context.GetHttpReq();
    req.Print();
}




// int main()
// {
//     //test_split();
//     //test_read_write();
//     //test_url();
//     //test_status_mime();
//     //test_isdir_isreg();
//     //test_path();
//     //test_context();
//     HttpServer httpserver(8989);
//     httpserver.SetBaseDir("./wwwroot");
//     httpserver.SetThreadCount(2);
//     httpserver.Listent();

//     return 0;
// }

#define WWWROOT "./wwwroot/"

std::string RequestStr(const HttpRequest &req) {
    std::stringstream ss;
    ss << req._method << " " << req._path << " " << req._version << "\r\n";
    for (auto &it : req._parmas) {
        ss << it.first << ": " << it.second << "\r\n";
    }
    for (auto &it : req._headers) {
        ss << it.first << ": " << it.second << "\r\n";
    }
    ss << "\r\n";
    ss << req._body;
    return ss.str();
}
void Hello(const HttpRequest &req, HttpResponse *rsp) 
{
    rsp->SetBody(RequestStr(req), "text/plain");
}
void Login(const HttpRequest &req, HttpResponse *rsp) 
{
    rsp->SetBody(RequestStr(req), "text/plain");
}
void PutFile(const HttpRequest &req, HttpResponse *rsp) 
{
    std::string pathname = WWWROOT + req._path;
    Util::WriteFile(pathname, req._body);
    //rsp->SetBody(RequestStr(req), "text/plain");
}
void DelFile(const HttpRequest &req, HttpResponse *rsp) 
{
    rsp->SetBody(RequestStr(req), "text/plain");
}
int main()
{
    HttpServer server(8989);
    server.SetThreadCount(3);
    server.SetBaseDir(WWWROOT);//设置静态资源根目录，告诉服务器有静态资源请求到来，需要到哪里去找资源文件
    server.SetGetHandler("/hello", Hello);
    server.SetPostHandler("/login", Login);
    server.SetPutHandler("/1234.txt", PutFile);
    server.SetDeleteHandler("/1234.txt", DelFile);
    server.Listent();
    return 0;
}
